<?php

namespace App\Service\Admin;

use App\Model\Admin\ClassesModel;
use App\Model\Admin\GradeModel;
use App\Model\Admin\WorkerGradeRelModel;
use App\Service\BaseService;

/**
 * 班级管理-服务类
 * @author LZH
 * @since: 2021/08/12
 * Class ClassesService
 */
class ClassesService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new ClassesModel();
        parent::__construct();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2021/08/12
     * @author LZH
     */
    public function getList()
    {
        // 查询条件
        $map = [];

        // 查询标题
        $title = $this->getRequestParam('title');
        if ($title) {
            $map[] = ['title' => ["%{$title}%",'like']];
        }

        // 获取年级
        $gradeModel = new GradeModel();
        $grades = $gradeModel->join('sc_worker_grade_rel', 'sc_worker_grade_rel.type_id=sc_grade.id')
            ->join('sc_worker', 'sc_worker.id = sc_worker_grade_rel.worker_id')
            ->where('type', 0)
            ->where('sc_grade.mark', 1)
            ->field(['sc_grade.id as id','sc_grade.name as name','school_year','dept_name','type',"group_concat(sc_worker.name , concat('(',duty_name, ')')) as worker_name",  'note'])
            ->group('id')
            ->all()->toArray(true, false);

        foreach ($grades as &$grade) {
            $classes = $this->model->join('sc_worker_grade_rel', 'sc_worker_grade_rel.type_id=sc_classes.id')
                ->join('sc_worker', 'sc_worker.id = sc_worker_grade_rel.worker_id')
                ->where('type', 1)
                ->where('sc_classes.mark', 1)
                ->where('grade_id', $grade['id'])
                ->field(['sc_classes.id as id', 'sc_classes.name as name','type',"group_concat(sc_worker.name , concat('(' ,subject_name, ')')) as worker_name", 'note'])
                ->group('id')
                ->all()->toArray(true, false);

            if (!empty($classes)) {
                array_walk($classes, function (&$item) use ($grade) {
                    $item = array_merge($item, [
                        'school_year' => $grade['school_year'],
                        'dept_name' => $grade['dept_name']
                    ]);
                });

                $grade['children'] = $classes;
            }
            $grade['id'] = 'grade-' . $grade['id'];
        }

        return $grades;
    }

    public function edit()
    {
        $id = parent::edit();

        $dynamicItem = $this->getRequestParam('dynamicItem'); // 保存到worker_grade_rel表中

        /**
         {
            "worker_id": 1,
            "duty_code": "ClassTeacher",
            "subject_code": "subjectType_yinle",
            "key": 1629195086359
        }
         */
        foreach ($dynamicItem as &$item) {
            $item['type'] = 1; // 班级与职工关系
            $item['type_id'] = $id;
            $temp = explode(':', $item['duty_code']);
            $item['duty_name'] = $temp[1];
            $item['duty_code'] = $temp[0];
            $temp = explode(':', $item['subject_code']);
            $item['subject_code'] = $temp[0];
            $item['subject_name'] = $temp[1];
        }

        $workerRoleRelModel = new WorkerGradeRelModel();
        $result = $workerRoleRelModel->saveAll($dynamicItem);
        return array_keys($result);
    }
}
